package com.vcs.frm.web.school;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.vcs.frm.domain.Designation;
import com.vcs.frm.domain.Teacher;
import com.vcs.frm.service.CommonService;
import com.vcs.frm.service.SchoolService;
import com.vcs.frm.web.form.school.NewTeacherForm;

@Controller
public class NewTeacherController {

	Log log = LogFactory.getLog(this.getClass());
	DateFormat formatter = new SimpleDateFormat("dd/mm/yyyy"); 
	
	@Autowired
	private CommonService commonService;
	
	@Autowired
	private SchoolService schoolService;

	/* Constructor */
	public NewTeacherController() {
	}

	public void setCommonService(CommonService commonService) {
		this.commonService = commonService;
	}
	
	@RequestMapping(value="/school/newTeacherForm", method = RequestMethod.GET)
	public ModelAndView newTeacherForm() {

		commonService.getUser(2);
		
		ModelAndView mav = new ModelAndView();

		NewTeacherForm formBean = new NewTeacherForm();
		mav.addObject("newTeacherForm", formBean);
		System.out.println("commonService : " + commonService);

		// get list of all teachers
		List<Teacher> teachers = schoolService.getAllTeachers();
		mav.addObject("teachers", teachers);

		// designation list in the form
		List<Designation> designationList = new ArrayList<Designation>();
		designationList = commonService.getDesignationList();
		mav.addObject("designationList", designationList);

		// View for new teacher form.
		mav.setViewName("newTeacherForm");
		return mav;
	}

	/**
	 * 
	 * @param newTeacherForm
	 * @param bindingResult
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/school/newTeacherForm", method = RequestMethod.POST)
	public ModelAndView processNewTeacherForm(
			@ModelAttribute("newTeacherForm") @Valid NewTeacherForm newTeacherForm,
			BindingResult bindingResult, Model model, HttpSession session) {
		System.out.println("\n\n>>>>>>>>"+newTeacherForm);
		ModelAndView mav = new ModelAndView();

		// designation list in the form
		List<Designation> designationList = new ArrayList<Designation>();
		designationList = commonService.getDesignationList();
		mav.addObject("designationList", designationList);

		// set form bean in request
		mav.addObject("newTeacherForm", newTeacherForm);
		
		if (bindingResult.hasErrors()) {
			log.info("-processNewTeacherForm - form was invalid :((((((((((((((((((((( ");
			// designation list in the form
			model.addAttribute("newTeacherForm", newTeacherForm);
			mav.setViewName("newTeacherForm");
			return mav;
		}
		log.info("-processNewTeacherForm form was valid :) --------------------");

		if (newTeacherForm != null
				&& (newTeacherForm.getFirstName() != null && newTeacherForm.getFirstName().trim() != "")) {
			// we will save teacher in database using schoolDao
			Teacher teacher = new Teacher();
			teacher.setFirstName(newTeacherForm.getFirstName());
			teacher.setMiddleName(newTeacherForm.getMiddleName());
			teacher.setLastName(newTeacherForm.getLastName());
			teacher.setDesignationCode(newTeacherForm.getDesignation());
			teacher.setQualifications(newTeacherForm.getQualifications());
			teacher.setSpecialCourses(newTeacherForm.getSpecialCourses());
			
			//convert string to date
			
			try {
				teacher.setDateOfJoining(formatter.parse(newTeacherForm.getDateOfJoining()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			schoolService.saveTeacher(teacher);
		}
		
		// get list of all teachers
		List<Teacher> teachers = schoolService.getAllTeachers();
		mav.addObject("teachers", teachers);
		
		// target view name
		mav.setViewName("listTeachers");

		mav.addObject("newTeacherForm", new NewTeacherForm());
		return mav;
	}
	
	
	/**
	 * Handels Teacher Edit request.
	 * @param newTeacherForm
	 * @param bindingResult
	 * @param model
	 * @param teacherId
	 * @return
	 */
	
	@RequestMapping(value="/school/editTeacher/{teacherId}", method = RequestMethod.GET)
	public ModelAndView teacherEditHandler(@ModelAttribute("newTeacherForm") NewTeacherForm newTeacherForm,
			BindingResult bindingResult, Model model, @PathVariable("teacherId")int teacherId){
		
		System.out.println("\n\nEdit teacher\n\n\n");
		ModelAndView mav = new ModelAndView();
		
		// designation list in the form
		List<Designation> designationList = new ArrayList<Designation>();
		designationList = commonService.getDesignationList();
		mav.addObject("designationList", designationList);
		
		// get list of all teachers
		List<Teacher> teachers = schoolService.getAllTeachers();
		mav.addObject("teachers", teachers);
		
		// get teacher that is to be edited.
		Teacher teacher = schoolService.getTeacherByTeacherId(teacherId);
		System.out.println(teacher.getFirstName()+" : first Name");
		newTeacherForm.setTeacherId(teacher.getTeacherId());
		newTeacherForm.setFirstName(teacher.getFirstName());
		newTeacherForm.setMiddleName(teacher.getMiddleName());
		newTeacherForm.setLastName(teacher.getLastName());
		newTeacherForm.setQualifications(teacher.getQualifications());
		newTeacherForm.setSpecialCourses(teacher.getSpecialCourses());
		newTeacherForm.setDesignation(teacher.getDesignationCode());
		newTeacherForm.setDateOfJoining(formatter.format((teacher.getDateOfJoining())));
				
		// target view name
		mav.setViewName("newTeacherForm");
		return mav;
	}
	
	/**
	 * Deletes a teacher
	 * @param teacherId
	 * @return
	 */
	
	@RequestMapping(value="/school/deleteTeacher/{teacherId}", method = RequestMethod.GET)
	public ModelAndView teacherDeleteHandler(@PathVariable("teacherId")int teacherId){
		
		ModelAndView mav = new ModelAndView();
		if(teacherId == 0){
			
			// target view name
			mav.setViewName("redirect:/school/teachers");
		}
		log.info("Deleting teacher with id : " + teacherId);

		schoolService.deleteTeacher(teacherId);
		
		
		// target view name
		mav.setViewName("redirect:/school/newTeacherForm");
		return mav;
	}

	/**
	 * List of Teachers
	 * @return
	 */
	@RequestMapping(value="/school/teachers")
	public ModelAndView listTeachers(){
		
		ModelAndView mav = new ModelAndView();
		
		// get list of all teachers
		List<Teacher> teachers = schoolService.getAllTeachers();
		mav.addObject("teachers", teachers);

		mav.setViewName("listTeachers");
		return mav;
	}
	
	/**
	 * Teacher's Profile in Detail
	 * @return
	 */
	@RequestMapping(value="/school/teachers/{teacherId}")
	public ModelAndView teachersDetailedProfile(@PathVariable("teacherId") int teacherId){
		
		ModelAndView mav = new ModelAndView();
		
		// get teacher's profile
		Teacher teacher = schoolService.getTeacherByTeacherId(teacherId);
		mav.addObject("teacher", teacher);

		mav.setViewName("teacherProfile");
		return mav;
	}
	
}
